package com.klcw.MuticastDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName MuticastServer
 * @Description 组播
 * @Author dukang
 * @Date 2019/1/21 14:55
 * @Version 1.0
 **/
public class MuticastServer {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 创建一个组名称 地址段  224.0.0.0 ---- 239.255.255.255
        InetAddress inetAddress = InetAddress.getByName("224.5.6.7");

        MulticastSocket socket = new MulticastSocket();

        for (int i = 0 ; i<10 ; i++){
            String data = "Hello dk";
            byte[] bytes = data.getBytes();
            // 使用UDP发送到组中  指点端口号
            socket.send(new DatagramPacket(bytes,bytes.length,inetAddress,8888));
            // TimeUnit 使用枚举  休眠
            TimeUnit.SECONDS.sleep(2);
        }
    }
}
